<?php

namespace maotool\core;

use maotool\core\console\Command;
use maotool\file\FileUtil;
use maotool\object\StrUtil;
use maotool\util\ContainerUtil;
use maotool\util\MaoToolUtil;

class Console
{
    public function run($argv = [])
    {
        echo "\n" . FileUtil::read("maotool:/src/main/resources/static/logo.txt");
        echo "版本号 " . MaoToolUtil::getVersion() . "\n";
        echo MaoToolUtil::getDescription() . " \n";
        echo "主页 " . MaoToolUtil::getHomepage() . " \n";
        echo "\n";
        $commands = ContainerUtil::getFatherClasss(Command::class);
        foreach ($commands as $command) {
            /** @var Command $command */
            if ($command->isHit($argv)) {
                $command->handle($argv);
                return;
            }
        }
        if (count($argv) > 1) {
            if ($argv[1] != "help") {
                echo "无效命令 请选择正确命令 \n\n";
            }
        } else {
            echo "请选择命令 \n\n";
        }
        foreach ($commands as $command) {
            /** @var Command $command */
            if (StrUtil::hasNotEmpty($command->getHelp($argv))) {
                echo $command->getHelp($argv) . "\n";
            }
        }
        echo "help 命令查看帮助\n";
    }

}